package solve;

import java.util.Stack;

import javax.swing.JFrame;

import data.cell;

public class mazesolve
{
	private int foundPathCounter = 0;
	
	private cell[][] maze;
	private cell currentCell;
	private cell tempCell;
	private cell foundCell = null;
	
	private Stack<cell> cellStack;
	
	private JFrame mainFrame;
	
	public mazesolve(cell[][] cells,JFrame mainFrame)
	{
		this.maze = cells;
		this.mainFrame = mainFrame;
	}
	
	public boolean solve()
	{
		currentCell = maze[0][63];
		
		//create a CellStack (LIFO) to hold a list of cell locations 
		cellStack = new Stack<cell>();
		
		//while currentCell is not Exit 
		while(!currentCell.isExit())
		{
		    //find all neighbors of CurrentCell with missing wall
			cell[] pathArray = new cell[4];
			foundPathCounter = 0;
			
			foundCell = null;
			
			for(int i =0;i<4;i++)
			{
				switch(i)
				{
					case 0: // North
					{
						if((currentCell.getY() - 1) < 0)
							break;
						else if(currentCell.isWallNorth() == false && !currentCell.isPathNorth() /*&& !currentCell.isBacktrackNorth()*/)
							foundCell = maze[currentCell.getX()][currentCell.getY()-1];
					}
					break;
					case 1: // East
					{
						if((currentCell.getX()+ 1) > 63)
							break;
						else if(currentCell.isWallEast() == false && !currentCell.isPathEast() /*&& !currentCell.isBacktrackEast()*/)
							foundCell = maze[currentCell.getX()+1][currentCell.getY()];
					}
					break;
					case 2: // South
					{
						if((currentCell.getY() + 1) > 63)
							break;
						else if(currentCell.isWallSouth() == false && !currentCell.isPathSouth() /*&& !currentCell.isBacktrackSouth()*/)
							foundCell = maze[currentCell.getX()][currentCell.getY()+1];
					}
					break;
					case 3: // West
					{
						if((currentCell.getX() - 1) < 0)
							break;
						else if(currentCell.isWallWest() == false && !currentCell.isPathWest() /*&& !currentCell.isBacktrackWest()*/)
							foundCell = maze[currentCell.getX()-1][currentCell.getY()];
					}
					break;
				}
				if(foundCell != null)
				{
					pathArray[foundPathCounter] = foundCell;
					foundPathCounter++;
				}
			}
			
		    //if one or more found 
			if(foundPathCounter > 0)
			{
				int direction = 0;
		        
				//choose one at random
				int randInt = (int)Math.round(Math.random()*(foundPathCounter - 1));				
				tempCell = pathArray[randInt];
				
				if(tempCell.getY() < currentCell.getY())
				{
					currentCell.setTo("North");
					direction = 0;
				}
				else if(tempCell.getX() > currentCell.getX())
				{
					currentCell.setTo("East");
					direction = 1;
				}
				else if(tempCell.getY() > currentCell.getY())
				{
					currentCell.setTo("South");
					direction = 2;
				}
				else if(tempCell.getX() < currentCell.getX())
				{
					currentCell.setTo("West");
					direction = 3;
				}
				switch(direction)
				{
					case 0: // South
						tempCell.setFrom("South");
						break;
					case 1: // West
						tempCell.setFrom("West");
						break;
					case 2: // North
						tempCell.setFrom("North");
						break;
					case 3: // East
						tempCell.setFrom("East");
						break;
				}
				
		        //push CurrentCell location on the CellStack 
				cellStack.push(currentCell);
				
		        //make the new cell CurrentCell 
				currentCell = tempCell;
				//System.out.println(currentCell.toString());
				mainFrame.repaint();
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else 
			{				
			    //pop the most recent cell entry off the CellStack 
		        //make it CurrentCell endIf
				//cell prevCell = currentCell;
				currentCell = cellStack.pop();
				
				/*//naar westen vanaf prevCell, oosten vanaf currentCell
				if(prevCell.getX() > currentCell.getX())
				{
					prevCell.setBacktrackWest(true);
					prevCell.setPathWest(false);
					
					currentCell.setBacktrackEast(true);
					currentCell.setPathEast(false);
				}
				// naar oosten vanaf prevCell, westen vanaf currentCell
				else if(prevCell.getX() < currentCell.getX())
				{
					prevCell.setBacktrackEast(true);
					prevCell.setPathEast(false);
				
					currentCell.setBacktrackWest(true);
					currentCell.setPathWest(false);	
				}
				// naar zuiden vanaf prevCell, noorden vanaf currentCell
				else if(prevCell.getY() > currentCell.getY())
				{
					prevCell.setBacktrackSouth(true);
					prevCell.setPathSouth(false);
				
					currentCell.setBacktrackNorth(true);
					currentCell.setPathNorth(false);	
				}
				// naar noorden vanaf prevCell, zuiden vanaf currentCell
				else if(prevCell.getY() < currentCell.getY())
				{
					prevCell.setBacktrackNorth(true);
					prevCell.setPathNorth(false);
				
					currentCell.setBacktrackSouth(true);
					currentCell.setPathSouth(false);	
				}
				*/
			}
		}
		
		return true;
	}
	
	public cell getCurrent()
	{
		return this.currentCell;
	}
	
	public void setCurrent(cell currentCell)
	{
		this.currentCell = currentCell;
	}
}